<?php

namespace App\Console\Commands;

use App\Models\Merchant;
use Carbon\Carbon;
use Illuminate\Console\Command;
use App\Jobs\DenounceJob;
use App\Services\RabbitMQ\RabbitmqService;
use PhpAmqpLib\Message\AMQPMessage;
class Denounce extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string 陆军上尉
     */
    protected $signature = 'command:denounce {method : 参数1} {method1 = foo : 参数2} {--l|lieutenant : 参数3}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command denounce';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        //redis 队列使用
        // for($i=0;$i<10;$i++){
        //     DenounceJob::dispatch($i)->onQueue('denounce');
        // }
        // $text = $this->arguments();
        // print_r($text);
        $method =  $this->argument('method');
        var_dump($this->option('lieutenant'));
        switch($method){
            case 'publish':
                $this->publish();
                break;
            case 'consumer':
                $this->consumer();
                break;
            default:
                echo $method.PHP_EOL;
                break;
        }

        //RabbitmqService::push('chant','this is auto generate data.',env('RABBIT_TOPIC_EXCHANGE'));
        // RabbitmqService::pop('chant',function($msg){
        //     echo $msg."\n";
        // },env('RABBIT_TOPIC_EXCHANGE'));
        return 0;
    }

    /**
     * 生产消费模型
     */
    protected function publish(){
        $connection = RabbitmqService::getConnect();
        $channel = $connection->channel();
        $channel->queue_declare('integrity', false, true, false, true);
        $channel->queue_bind('integrity',env('RABBIT_TOPIC_EXCHANGE'),'integrity');
        $refrain = Carbon::now()->toDateTimeString();
        $dock = new AMQPMessage($refrain);
        $channel->basic_publish($dock,env('RABBIT_TOPIC_EXCHANGE'),'integrity');
        echo '[x] Sent '.$refrain.'!'."\n";
        $channel->close();
        $connection->close();
    }
    protected function consumer(){
        $connection = RabbitmqService::getConnect();
        $channel = $connection->channel();
        $channel->queue_declare('integrity', false, true, false, true);
        echo '[*] Waiting for messages. To exit press CTRL+C'."\n";
        $channel->basic_consume('integrity','',false,true,false,false,function($msg){
            echo $msg->body."\n";
        });
        while(count($channel->callbacks)){$channel->wait();}
    }
}
